package org.example;
import java.sql.Timestamp;

/**
 * 交易记录实体类
 */
public class Transaction {
    private String transactionId;    // 交易流水号
    private String accountId;       // 关联账户
    private String type;            // 交易类型
    private double amount;          // 交易金额
    private Timestamp transactionTime; // 交易时间
    private String targetAccount;   // 目标账户(转账)
    private double balanceAfter;    // 交易后余额
    private String currentAccountId; // 当前查询的账户 ID

    // 新增构造函数，用于设置当前查询的账户 ID
    public Transaction(String currentAccountId) {
        this.currentAccountId = currentAccountId;
    }

    // Getter和Setter方法
    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public Timestamp getTransactionTime() {
        return transactionTime;
    }

    public void setTransactionTime(Timestamp transactionTime) {
        this.transactionTime = transactionTime;
    }

    public String getTargetAccount() {
        return targetAccount;
    }

    public void setTargetAccount(String targetAccount) {
        this.targetAccount = targetAccount;
    }

    public double getBalanceAfter() {
        return balanceAfter;
    }

    public void setBalanceAfter(double balanceAfter) {
        this.balanceAfter = balanceAfter;
    }

    @Override
    public String toString() {
        // 根据type的值，将type转换为对应的中文描述
        String typeStr = switch (type) {
            case "DEPOSIT" -> "存款";
            case "WITHDRAW" -> "取款";
            case "TRANSFER" -> isIncomingTransfer() ? "转入" : "转出";
            default -> type;
        };

        // 格式化描述字符串，包括交易时间、类型、金额
        String desc = String.format("[%s] %s: %.2f元",
                transactionTime, typeStr, amount);

        // 如果类型为TRANSFER，则根据是否是转入交易，添加对应的描述
        if ("TRANSFER".equals(type)) {
            desc += isIncomingTransfer()
                    ? String.format(" 来自: %s", accountId)
                    : String.format(" 至: %s", targetAccount);
        }
        // 添加余额描述
        desc += String.format(" | 余额: %.2f元",
                balanceAfter);

        // 返回描述字符串
        return desc;
    }

    // 判断是否为转入交易
    private boolean isIncomingTransfer() {
        // 如果目标账户不为空且目标账户等于当前账户ID，则返回true，否则返回false
        return targetAccount != null && targetAccount.equals(currentAccountId);
    }
}